package fun.linze.wallpaperwebsite.controller;


import fun.linze.wallpaperwebsite.dao.user.RegisterDAO;
import fun.linze.wallpaperwebsite.dao.user.UserInfoDAO;
import fun.linze.wallpaperwebsite.dao.user.UserLoginDAO;
import fun.linze.wallpaperwebsite.service.impl.TUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class UserController {

    @Autowired
    TUserServiceImpl tUserService;

//    POST /api/v1/register
//    功能: 处理用户注册请求，创建新用户。
    @PostMapping("/register")
    public ResponseEntity<?> Register(@RequestBody RegisterDAO registerDAO) {
        // 处理用户注册逻辑
        if (!tUserService.registerUser(registerDAO)) {
            return ResponseEntity.badRequest().body("用户名或邮箱已存在");
        }
        UserInfoDAO userInfoDAO = tUserService.getUserInfo(registerDAO.getUsername());
        URI uri = URI.create("/api/v1/register/" + userInfoDAO.getUsername());
        return ResponseEntity.created(uri).body(userInfoDAO);
    }
//    POST /api/v1/login
//    功能: 处理用户登录请求，验证凭据并返回 Token。
    @PostMapping("/login")
    public ResponseEntity<?> Login(@RequestBody UserLoginDAO userLoginDAO) {
        String token = tUserService.login(userLoginDAO.getUsername(), userLoginDAO.getPassword());
        if (token == null) {
            return ResponseEntity.badRequest().body("用户名或密码错误");
        }
        Map<String, String> result = new HashMap<>();
        result.put("token", token);
        return ResponseEntity.ok().body(result);
    }

//    GET /api/v1/me
//    功能: 获取当前认证用户的详细信息。
    //从token中获取用户名，并查询用户信息
    @GetMapping("/me")
    public ResponseEntity<?> getCurrentUserInfo(@RequestHeader("Authorization") String authHeader) {
        String token = authHeader.replace("Bearer ", "");
        String username = tUserService.getUsernameFromToken(token);
        if (username == null) {
            return ResponseEntity.status(401).body("未认证或Token无效");
        }
        UserInfoDAO userInfoDAO = tUserService.getUserInfo(username);
        if (userInfoDAO == null) {
            return ResponseEntity.status(404).body("用户不存在");
        }
        return ResponseEntity.ok().body(userInfoDAO);
    }
//    PUT /api/v1/me
//    功能: 更新当前认证用户的个人信息（目前仅可更新昵称）。
    @PutMapping("/me")
    public ResponseEntity<?> updateCurrentUserInfo(@RequestHeader("Authorization") String authHeader, @RequestBody UserInfoDAO userInfoDAO) {
        // 从请求头中获取 Token
        //从TOKEN中提取用户名
        String token = authHeader.replace("Bearer ", "");
        String username = tUserService.getUsernameFromToken(token);
        if (username == null) {
            return ResponseEntity.status(401).body("未认证或Token无效");
        }
        if( userInfoDAO.getUsername() == null || !userInfoDAO.getUsername().equals(username)) {
            return ResponseEntity.badRequest().body("用户名不匹配");
        }
        // 更新用户信息逻辑
        boolean updated = tUserService.updateUserInfo(username, userInfoDAO);
        if (!updated) {
            return ResponseEntity.badRequest().body("更新失败");
        }
        return ResponseEntity.ok().body("更新成功");
    }
    //todo:更新用户头像，更新邮箱，注销账号，账号合并


    //todo:(需要添加数据表)用户点赞，收藏，用户壁纸浏览记录，统计用户喜好

}
